__author__ = 'Kerr Wang'

import json
import time
import DataAnalyze
import HttpPost

class Recorder(object):
	def __init__(self, recoderTuple, cur_time):
		self.num = recoderTuple[0]
		self.machineNum = recoderTuple[1]
		self.is_replace = recoderTuple[2]
		self.count = recoderTuple[3]
		self.currentTime = (int)(cur_time*1000)

def recorder_to_json(r):
	return {
		'code':r.num,
		'terminal':r.machineNum,
		'date':r.currentTime,
		'station': 'All_in_One',
		'count': r.count,
		'isBeat':False,
		'isStationBeat':False
	}

class Beat(object):
	def __init__(self, machineNum):
		self.num = '00000000'
		self.machineNum = machineNum
#		self.is_replace = False
		self.count = 170
		self.currentTime = (int)(time.time()*1000)

def beat_to_json(b):
	return {
		'code':b.num,
		'terminal':b.machineNum,
		'date':b.currentTime,
		'station': 'All_in_One',
		'count': b.count,
		'isBeat':True,
		'isStationBeat':False
	}

def beat_to_jsonList(machineNum):
	list_json = []
	list_json.append(Beat(machineNum))
	dataJson = json.dumps(list_json, default=beat_to_json)
	return dataJson

def list_to_json(listData,cur_time):
	if not isinstance(listData, (list, tuple)):
		raise TypeError('bad operand type, parameter must be a tuple or list')
	list_json = []
	for elem in listData:
		list_json.append(Recorder(elem,cur_time))
	dataJson = json.dumps(list_json, default=recorder_to_json)
	return dataJson
#基站心跳包
class Station_Beat(object):
	def __init__(self, machineNum):
		self.num = '00000000'
		self.machineNum = machineNum
		self.is_replace = False
		self.count = 170
		self.currentTime = (int)(time.time()*1000)

def Station_beat_to_json(a):
	return {
		'code':a.num,
		'terminal':a.machineNum,
		'date':a.currentTime,
		'station': 'All_in_One',
		'count': a.count,
		'isBeat':True,
		'isStationBeat':True
	}

def Station_beat_to_jsonList(machineNum):
	list_json = []
	list_json.append(Station_Beat(machineNum))
	dataJson = json.dumps(list_json, default=Station_beat_to_json)
	return dataJson

def main():
	t = (0x0A,0x06, 0x91, 0x04, 0x78, 0x78, 0x41, 0x01, 0x0B,
	    0x0A, 0x06, 0x12, 0x04, 0x78, 0x21, 0x41, 0x01, 0x0B,  
	    0x0A, 0x06, 0x13, 0x04, 0x78, 0x22, 0x41, 0x02, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0A, 0x06, 0x14, 0x04, 0x78, 0x24, 0x41, 0x03, 0x0B,
	    0x0B, 0x0A, 0xFE)
	if DataAnalyze.data_correct(t, 9):
		list_data,d = DataAnalyze.data_analyze(t, 9)
		#print (list_data,d )#('11047878', 65, True, 1)
#		dataJson = list_to_json(list_data,time.time())
		dataJson = Station_beat_to_jsonList(0)
		print (dataJson)#{"date": 1479176917264, "code": "11047878", "station": "All_in_One", "count": 1, "isStationBeat": false, "isBeat": false, "terminal": 65}
#		HttpPost.httpSend('localhost', 8000, dataJson)、


if __name__ == '__main__':
	main()
